package com.code.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.code.dao.OrderSettingDao;
import com.code.pojo.OrderSetting;
import com.code.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 业务层实现类
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    // 添加
    @Override
    public void add(List<OrderSetting> list) {
        if (list != null && list.size() > 0){
            for (OrderSetting orderSetting : list) {
                Date orderDate = orderSetting.getOrderDate();
                // 查询此日期是否设置了预约
                long count = orderSettingDao.findCountByOrderDate(orderDate);
                if (count > 0){
                    // 设置了就更新数据
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else {
                    // 没设置就添加数据
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    // 根据日期查询预约设置数据
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String dateBegin = date + "-1";
        String dateEnd = date + "-31";
        Map map = new HashMap();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        ArrayList<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            HashMap orderSettingMap = new HashMap<>();
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());
            orderSettingMap.put("number",orderSetting.getNumber());
            orderSettingMap.put("reservations",orderSetting.getReservations());
            data.add(orderSettingMap);
        }
        return data;
    }

    // 更新
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0){
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {
            orderSettingDao.add(orderSetting);
        }
    }

}
